module.exports = function ($scope, $resource, toaster, $modalInstance, $stateParams, FileUploader, product_code) {
  $scope.pageInfo = {
      asrot: 0,
      product: {}
  }
  $scope.info = {};
  /* 分页
   * ========================================= */
  $scope.maxSize = 5;            //最多显示多少个按钮
  $scope.bigCurrentPage = 1;      //当前页码
  $scope.itemsPerPage = 10; 
  $scope.load = function () {
      var para = {
          pageNo:$scope.bigCurrentPage, 
          pageSize:$scope.itemsPerPage,
          product_code:product_code
      };
      $resource('/api/as/mc/shopclass/pageChooseList', {}, {}).save(para, function (res) {
          if (res.errcode === 0) {
              $scope.classList = res.data.results;
              $scope.bigTotalItems = res.data.totalRecord;
              $scope.classList.forEach(element => {
                if (element.choose == '0'){
                  element.selected = true;
                } else {
                  element.selected = false;
                }
              });
          } else {
              toaster.error({ title: '', body: res.errmsg });
          }
      });
  }
  $scope.load();
  $scope.bean = {
    'selected' : false
  }
  var url = '';
  $scope.change = function(obj){
      console.log(obj);
      $scope.info = {
        'product_code' : product_code,
        'class_code' : obj.class_code
      }
      if (obj.selected == true){
        url = '/api/as/mc/shopclass/saveClassProduct';
      } else {
        url = '/api/as/mc/shopclass/deleteClassProduct';
      }
      $resource(url, {}, {}).save($scope.info, function (res) {
        if (res.errcode === 0) {
            toaster.success({ title: '', body: '操作成功' });
            // $modalInstance.close();
        } else {
            toaster.error({ title: '', body: res.errmsg });
        }
    });
      // if(obj.mark == true){
      //     $scope.typelist.push(obj)
      // }else{
      //     for(var i = 0;i<$scope.typelist.length;i++){
      //         if(obj.class_code == $scope.typelist[i].class_code){
      //             $scope.typelist.splice(i,1);
      //         }
      //     }
      // }
      // console.log('下面是typelist');
      // $scope.typelist.forEach(element => {
      //   element.product_code = product_code;
      // });
      // console.log($scope.typelist);
      
  }

  //添加分类下产品
  $scope.save = function () {
      let para = {
        insertlist: $scope.typelist
      }
      console.log(para);
      $resource('/api/as/mc/shopclass/saveClassProduct', {}, {}).save(para, function (res) {
          if (res.errcode === 0) {
              toaster.success({ title: '', body: '操作成功' });
              $modalInstance.close();
          } else {
              toaster.error({ title: '', body: res.errmsg });
          }
      });
  }


  //关闭
  $scope.ok = function () {
      $modalInstance.close();
  }

  //修改
  $scope.update = function (info) {
      info.update_state = false;
  }

  //保存
  $scope.saveone = function (info) {
      
      $resource('/api/as/mc/shopclass/updateClassProduct', {}, {}).save(info, function (res) {
          if (res.errcode === 0) {
              toaster.success({ title: '', body: '操作成功' });
              $scope.load();
          } else {
              toaster.error({ title: '', body: res.errmsg });
          }
      });
  }


  //取消
  $scope.close = function () {
      $modalInstance.dismiss('cancel');
  }

};